package me.gacl.web.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.gacl.domain.User;

/**
 * Servlet implementation class RegisterServlet
 */
/*
 *编写处理用户注册的Servlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		 try{
	            String username = request.getParameter("username");
	            username = new String(request.getParameter("username").getBytes("iso-8859-1"), "utf-8");
	            String password = request.getParameter("password");
	            String email = request.getParameter("email");
	            User user = new User();
	            user.setEmail(email);
	            user.setPassword(password);
	            user.setUsername(username);
	            
	            System.out.println("把用户信息注册到数据库中");
	            //用户注册成功之后就使用用户注册时的邮箱给用户发送一封Email
	            //发送邮件是一件非常耗时的事情，因此这里开辟了另一个线程来专门发送邮件
	            Sendmail send = new Sendmail(user);
	            //启动线程，线程启动之后就会执行run方法来发送邮件
	            send.start();
	            
	            //注册用户
	            //new UserService().registerUser(user);
	            request.setAttribute("message", "恭喜您，注册成功，我们已经发了一封带了注册信息的电子邮件，请查收，如果没有收到，可能是网络原因，过一会儿就收到了！！");
	            request.getRequestDispatcher("/message.jsp").forward(request, response);
	        }catch (Exception e) {
	            e.printStackTrace();
	            request.setAttribute("message", "注册失败！！");
	            request.getRequestDispatcher("/message.jsp").forward(request, response);
	        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
